package com.qsmx.store.app.main.goods;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.event.GoodsOrderRefreshEvent;
import com.qsmx.store.app.event.HomeUnLockEvent;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.gift.entity.GiftEntity;
import com.qsmx.store.app.main.goods.adapter.goodsorder.GoodsOrderPanelListLayout;
import com.qsmx.store.app.main.goods.adapter.goodsorder.GoodsOrderTableListAdapter;
import com.qsmx.store.app.main.goods.entity.GoodsEntity;
import com.qsmx.store.app.main.goods.entity.GoodsOrderEntity;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import butterknife.BindView;

public class GoodsOrderDelegate extends AtmosDelegate {

    private GoodsOrderTableListAdapter mAdapter;

    private GoodsOrderEntity mData;
    private List<String> rowDataList = new ArrayList<>();
    private List<Integer> itemWidthList = new ArrayList<>();
    private List<GoodsOrderEntity.ContentBean> contentList = new ArrayList<>();

    private List<String> goodsRowDataList = new ArrayList<>();
    private List<Integer> goodsItemWidthList = new ArrayList<>();

    @BindView(R2.id.pl_root)
    GoodsOrderPanelListLayout plRoot;
    @BindView(R2.id.lv_xls)
    ListView lvXls;

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_order_search;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        initOrderData();
        initRowDataList();
        initItemWidthList();
        initAdapter();
        plRoot.setAdapter(mAdapter);
        plRoot.getAdapter().setContentData(contentList);

    }

    private void initOrderData() {

        contentList = new ArrayList<>();

        WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("businessDate", FustionManager.mMachineData.getBusinessDate());
        params.put("deptId", FustionManager.mLoginData.getUser().getUser().getDept().getId());

        HttpHelper.RestClientGet(params, HttpUrl.GOODS_SUBMIT,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {
                        Type type = new TypeToken<GoodsOrderEntity>() {
                        }.getType();
                        Gson gson = new Gson();
                        mData = gson.fromJson(response, type);
                        contentList = mData.getContent();
                        plRoot.getAdapter().setContentData(contentList);
                        plRoot.getAdapter().notifyDataSetChanged();
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });

    }

    private void initAdapter() {
        mAdapter = new GoodsOrderTableListAdapter(getContext(), plRoot, lvXls, R.layout.adapter_gift_list);
        mAdapter.setTitleWidth(0);
        mAdapter.setTitleHeight(40);
        mAdapter.setRowColor("#04b098");
        mAdapter.setSwipeRefreshEnabled(false);
        mAdapter.setOnRefreshListener(new CustomRefreshListener());// optional
        mAdapter.setRowDataList(rowDataList);// must have
        mAdapter.setItemWidthList(itemWidthList);// must have
        mAdapter.setItemHeight(40);// optional, dp

    }

    /**
     * 生成一份横向表头的内容
     *
     * @return List<String>
     */
    private void initRowDataList() {
        rowDataList.add(getString(R.string.order_id));
        rowDataList.add(getString(R.string.order_detail));
        rowDataList.add(getString(R.string.order_time));
        rowDataList.add(getString(R.string.order_do_person));
        rowDataList.add(getString(R.string.remark));
        rowDataList.add(getString(R.string.order_pay_status));
        rowDataList.add(getString(R.string.order_pay_type));
        rowDataList.add(getString(R.string.order_old_money));
        rowDataList.add(getString(R.string.order_true_money));
        rowDataList.add(getString(R.string.order_ic_num));
        rowDataList.add(getString(R.string.order_mem_num));
        rowDataList.add(getString(R.string.order_do));

        goodsRowDataList.add(getString(R.string.goods_name));
        goodsRowDataList.add(getString(R.string.goods_from));
        goodsRowDataList.add(getString(R.string.goods_sku));
        goodsRowDataList.add(getString(R.string.goods_type));
        goodsRowDataList.add(getString(R.string.goods_pay_money_type));
        goodsRowDataList.add(getString(R.string.goods_num));
        goodsRowDataList.add(getString(R.string.goods_cost_money));
        goodsRowDataList.add(getString(R.string.goods_sale_all_money));
        goodsRowDataList.add(getString(R.string.goods_off_money));
        goodsRowDataList.add(getString(R.string.goods_true_get_money));

    }

    private void initItemWidthList() {
        itemWidthList.add(200);
        itemWidthList.add(100);
        itemWidthList.add(200);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);
        itemWidthList.add(100);

        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);
        goodsItemWidthList.add(100);

    }

    public class CustomRefreshListener implements SwipeRefreshLayout.OnRefreshListener {
        @Override
        public void onRefresh() {
            // don`t forget to call this
            mAdapter.getSwipeRefreshLayout().setRefreshing(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(GoodsOrderRefreshEvent event) {
        if (event != null && event.getData() != null) {
            initOrderData();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
